import dataList from "@/components/dataList";
import { paymentPage } from "@/views/wy-finance/bill/api/actual";
import editRefund from "../edit/edit-refund";
import { downloadBlob } from "@/utils";
import { download } from "@/admin/api/common";
import moment from "moment";

export default {
	name: "resource-manage",
	components: { dataList, editRefund },
	provide() {
		return {
			initList: this.getListData
		};
	},
	beforeMount() {
		this.getListData();
	},
	data() {
		return {
			tableData: [],
			// 请求参数
			params: {
				current: 1,
				size: 20,
				projectName: ""
			},
			// 选中的数据
			selectionList: [],
			// 分页
			pager: {
				total: 0, // 总页数
				current: 1, // 当前页数
				size: 20 // 每页显示多少条
			},
			// 页面loading
			loading: false,
			filterList: [
				{
					label: "收款项目",
					inputType: "select",
					name: "chargeProjectName",
					multiple: true,
					children: [
						{
							label: "租金",
							value: "租金"
						},
						{
							label: "租赁押金",
							value: "租赁押金"
						},
						{
							label: "装修保证金",
							value: "装修保证金"
						}
					]
				},
				{
					label: "最小实收金额",
					inputType: "input",
					name: "collectionAmountStart"
				},
				{
					label: "最大实收金额",
					inputType: "input",
					name: "collectionAmountEnd"
				}
			],
			editDataShow: false
		};
	},
	filters: {
		mySubString(val) {
			if (val != null) {
				return val.substring(5, 7);
			}
			return "";
		}
	},
	methods: {
		getListData(params = {}) {
			this.loading = true;
			let requestData = { ...this.params, ...params };
			paymentPage(requestData)
				.then(res => {
					const { current, size, total, records } = res.data.data;
					this.tableData = records;
					this.params = requestData;
					this.pager = { total, current, size };
				})
				.finally(() => {
					this.loading = false;
				});
		},
		editRefund() {
			if (this.selectionList.length === 1) {
				if (this.selectionList[0].chargeProjectName === "租金") {
					this.err("收款项目为租金时不能回收押金！");
				} else {
					this.editDataShow = true;
					this.$nextTick(() => {
						this.$refs.editRefund.show(this.selectionList[0]);
					});
				}
			} else {
				this.err("请选择一个要收款的项目");
			}
		},
		dateFormat: function(row, column, cellValue) {
			if (!cellValue) {
				return "";
			}
			return moment(cellValue).format("YYYY-MM-DD");
		},
		downloadData() {
			this.publicExport(
				"实收",
				"/api/lessorpayment/downloadExcel",
				this.params
			);
		},
		handleClose() {
			this.editDataShow = false;
		},
		// 菜单点击监听
		handleMenuClick(type) {
			switch (type) {
				case "test":
					this.editRefund();
					break;
				case "export":
					this.downloadData();
					break;
				default:
					this.$message(type);
					break;
			}
		}
	}
};
